﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Runtime.CompilerServices;

namespace WcfRestSql
{
    // wcf_rest_sqlcommand matches class used to create json/xml client payload.
    [DataContract(Namespace = "")]
    public class wcf_rest_sqlcommand // simple class for stored proc or cmd info passed from client
    {
        [DataMember]
        public string command { get; set; }     // for stored proc, name of stored proc, for command text of command
        [DataMember]
        public string commandtype { get; set; } // either "text" or "storedprocedure" blank defaults to storedprocedure
        [DataMember]
        public string connect { get; set; }     // connect string name for matching service connectionstring
        [DataMember]
        public string paramnames { get; set; }  // paramater names delimited list for sp only, not text command
        [DataMember]
        public string paramvalues { get; set; } // parameter values delimited list for sp only, not text command
        [DataMember]
        public object[] paramlist { get; set; } //mảng các biến params, nếu insert nhiều row =>
        //public object[][] paramlist dùng để 
        [DataMember]
        public string executetype { get; set; } // can be "scaler" or "nonquery" or blank for default (execute reader)
        [DataMember]
        public int numberbatch { get; set; }
        [DataMember]
        public bool jsonformat { get; set; }

        public wcf_rest_sqlcommand() // public constructor
        {
            command = "";
            commandtype = "";
            paramvalues = "";
            paramnames = "";
            paramlist = null;
            connect = "";
            executetype = "";
            numberbatch = 1;
            jsonformat = true;
        }
    }

    // IWcfRestSql contains definitions for 3 service methods based on required return types
    [ServiceContract]
    public interface IWcfRestSql
    {
        [OperationContract] // runSqlSpDS call stored proc, returns Data Set
        [WebInvoke(UriTemplate = "/RunSqlDS", Method = "POST",
             RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Xml,

            BodyStyle = WebMessageBodyStyle.Bare)]
        DataSet runSqlDS(wcf_rest_sqlcommand cmd);

        [OperationContract] // runSqlSpXml call stored proc, returns Xml
        [WebInvoke(UriTemplate = "/RunSqlXml", Method = "POST",
           RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare)]
        String runSqlXml(wcf_rest_sqlcommand cmd);

        [OperationContract] // runSqlSpJson call stored proc, expects Json, returns Json
        [WebInvoke(UriTemplate = "/RunSqlJson", Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        String runSqlJson(wcf_rest_sqlcommand cmd);
    }
}